/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is COID http service.
 *
 * The Initial Developer of the Original Code is
 * PosAm.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Brano Kemen
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
 
#ifndef __COID_SERVICES_HTTPSERV__HEADER_FILE__
#define __COID_SERVICES_HTTPSERV__HEADER_FILE__


#include "coid/svc/coidsvc.h"

#include "coid/comm/binstream/netstreamtcp.h"
#include "coid/comm/binstream/filestream.h"
#include "coid/comm/hash/hashkeyset.h"
#include "mime.h"
#include "coid/server/tunnelmgr/client/tunnelmgr_client.h"

#include <sys/stat.h>

#define _LOG_REQUESTS 1

namespace coid {

////////////////////////////////////////////////////////////////////////////////
///Http server service
class HttpServ
{
    //NET_OPTIONS: acceptor_port 55080 acceptor_bound_session nolog_system_calls
    //NET_RELEASE: 0.1
public:

    HttpServ();

    net_fnc_s opcd accept_connect( net_account account_id& acc )
    {
        return 0;
    }

    net_fnc_s opcd accept_connect_shared()
    {
        return ersDENIED;
    }

    net_fnc_s opcd accept_net_connections( netSocket* socket, uint port );

    net_fnc_s opcd accept_startup_params( cmd_interface& cmd )
    {
        _cmdin = &cmd;

        if( _srvaddr.is_empty() )
            _cmdin->get_server_address(_srvaddr);

        _cmdin->get_working_dir(_cwd);
        directory::treat_trailing_separator( _cwd, false );
        _cwdlen = _cwd.len();

        _http.set_cache_size( 4096, 4096 );

        return 0;
    }

protected:

    opcd process( netstreamtcp& net );

    const charstr& get_mime_type( token fn ) const;

    opcd send_error( const token& err );
    opcd send_file();


    static MIME_REG  _mimereg;    
    static charstr _srvaddr;

    httpstreamcoid _http;

    charstr _cwd;
    uints _cwdlen;
    cmd_interface* _cmdin;

    TunnelMgr_client _tun;


    ///File request entry
    struct file_req
    {
        charstr _file;
        uint _count_ok;
        uint _count_fail;

        file_req() : _count_ok(0), _count_fail(0) {}
        explicit file_req( const token& t ) : _file(t), _count_ok(0), _count_fail(0) {}

        operator token() const     { return _file; }
    };

    struct file_req_sort
    {
        bool operator () ( const file_req* a, const file_req* b )
        {
            return (a->_count_ok + a->_count_fail) < (b->_count_ok + b->_count_fail);
        }
    };

    typedef hash_keyset<file_req, _Select_Copy<file_req,token> >   t_frhash;
    static t_frhash  _hash;
    static comm_mutex _hashmutex;


#ifdef _LOG_REQUESTS
    static comm_mutex _logmutex;
    static bofstream _logfile;
    binstreambuf _logb;
    txtstream _log;
#endif
};

} // namespace coid

#endif //__COID_SERVICES_HTTPSERV__HEADER_FILE__
